package com.ljh.cvsspringboot.hander;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

/**
 * @author 27013
 **/

@Component
public class SystemSessionHandler implements HandlerInterceptor {
    private static final Logger log= LoggerFactory.getLogger(SystemSessionHandler.class);

    /**
     * 在请求处理之前调用，只有返回true才会继续调用后续的Interceptor和Controller
     * @param request
     * @param response
     * @param handler
     * @return
     * @throws Exception
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        Object user=request.getSession().getAttribute("user");
        if(user==null){
            //没有登录，进行拦截
            log.warn("请求路径：{}，当前用户未登录，请先登录",request.getRequestURI());
            request.getRequestDispatcher("/toLogin").forward(request,response);
            return false;
        }
        return true;
    }
}

